package com.yuanlrc.base.service.admin;

import com.yuanlrc.base.bean.PageBean;
import com.yuanlrc.base.dao.admin.BookDao;
import com.yuanlrc.base.entity.admin.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookDao  bookDao ;

    /**
     * 根据图书id查询
     * @param id
     * @return
     */
    public Book find(Long id){
        return  bookDao.find(id);
    }

    /**
     * 按照图书名查找图书
     * @param bookName
     * @return
     */
    public Book findByBookName(String bookName){
        return bookDao.findByBookName(bookName);
    }

    /**
     * 图书添加/编辑操作
     * @param book
     * @return
     */
    public Book save(Book book){
        return bookDao.save(book);
    }

    /**
     * 分页查询图书列表
     * @param book
     * @param pageBean
     * @return
     */
//    public PageBean<Book> findList(Book book,PageBean<Book> pageBean){
//        ExampleMatcher withMatcher = ExampleMatcher.matching().withMatcher("bookName", ExampleMatcher.GenericPropertyMatchers.contains());
//        withMatcher = withMatcher.withIgnorePaths("state","num","cannum");
//        Example<Book> example = Example.of(book,withMatcher);
//        Pageable pageable = PageRequest.of(pageBean.getCurrentPage()-1, pageBean.getPageSize());
//        Page<Book> findAll = bookDao.findAll(example, pageable);
//        pageBean.setContent(findAll.getContent());
//        pageBean.setTotal(findAll.getTotalElements());
//        pageBean.setTotalPage(findAll.getTotalPages());
//        System.out.println(findAll.getContent());
//        return pageBean;
//    }

    public PageBean<Book> findList(Book book,PageBean<Book> pageBean){
        ExampleMatcher withMatcher = ExampleMatcher.matching()
                .withMatcher("bookName", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("author",ExampleMatcher.GenericPropertyMatchers.contains());
        withMatcher = withMatcher.withIgnorePaths("state","num","cannum");
        Example<Book> example = Example.of(book, withMatcher);
        Pageable pageable = PageRequest.of(pageBean.getCurrentPage()-1, pageBean.getPageSize());
        Page<Book> findAll = bookDao.findAll(example, pageable);
        pageBean.setContent(findAll.getContent());
        pageBean.setTotal(findAll.getTotalElements());
        pageBean.setTotalPage(findAll.getTotalPages());
        System.out.println(findAll.getContent());
        return pageBean;
    }

    /**
     * 查询所有的父分类 老大
     * @return
     */
    public List<BookCategory>  findparent (){
        List<BookCategory> parent = bookDao.findParent();
        return   parent ;
    }


    /**
     * 判断图书名是否存在，添加和编辑均可判断
     * @param bookName
     * @param id
     * @return
     */
    public boolean isExistbookName(String bookName,Long id){
        Book book = bookDao.findByBookName(bookName);
        if(book != null)
            //表示图书存在，接下来判断是否是编辑图书的本身
            if(book.getId().longValue() != id.longValue()) {
                return true;
            }
        return false;
    }

     public List<Book> findAll () {
          return  bookDao.findAll() ;
     }

    /**
     * 按照图书id删除
     * @param id
     */
    public void delete(Long id){
        bookDao.deleteById(id);
    }

    /**
     * 返回图书总数
     * @return
     */
    public long total(){
        return bookDao.count();
    }

    public List<BookCategory> findCategoryByParentId(Long id) {
        return bookDao.findCategoryByParentId(id);
    }
}
